package de.herberlin.pss;

import java.io.File;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;

import javax.swing.ImageIcon;

import org.apache.log4j.Logger;

import de.herberlin.pss.ui.Frame;

public abstract class ServiceLocator {
 
	private static File sqliteDir=null; 
	private static Logger logger=Logger.getLogger(ServiceLocator.class);
	
	public static File getSqliteFile() {
		return new File(getSqliteDir(),"MyDb");
	}
	
	public static File getSqliteDir() {
		return sqliteDir;
	}
	public static void setSqliteDir(File file) {
		
		sqliteDir=file.getParentFile();
		getPreferences().put(Frame.KEY_DEFAULT_PSS_DIR, sqliteDir.getAbsolutePath());
	}
	
	
	public static DatabaseManager getDatabaseManager() {
		return DatabaseManager.getInstance(getSqliteFile());
	}
	
	public static Preferences getPreferences() {
		return Preferences.userNodeForPackage(Main.class);
	}
	
	/**
	 * Resource handling
	 * @param key
	 * @return
	 */
	public static String getText(String key) {
		if (_bundle == null) {
			loadBundle(Locale.getDefault());
		}
		return _bundle.getString(key);
	}
	
	private static ResourceBundle _bundle=null;
	private static void loadBundle(Locale locale) {
		
		ResourceBundle bundle=ResourceBundle.getBundle("pss",locale);
		if (bundle==null) {
			logger.error("ResourceBundle must not be null");
		}
		_bundle =bundle;
	}
	
	public static ImageIcon getImage(String key) {
		ImageIcon icon=new ImageIcon(ImageIcon.class.getResource("/pic/"+key+"16.gif"));
		return icon;
	}
}
